Shader "Custom/specular1" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_DiffuseColor("Diffuse Color",Color) = (1,1,1,1)
		_DiffuseIntensity("Diffuse Intensity",float) = 1
		
		_SpecularColor("SpecularColor",Color) = (1,1,1,1)
		_SpecularIntensity("Specular Intensity",float) = 1
		
		_SpecularPower("_SpecularPower",float) = 128
		_LightIntensity("LightIntensity",float) = 1
	}
	
	CGINCLUDE
	#include "UnityCG.cginc"
	float4 _DiffuseColor;
	float _DiffuseIntensity;
	
	float4 _SpecularColor;
	float _SpecularIntensity;
	
	float _SpecularPower;
	float _LightIntensity;
	float4 _LightColor0;
	
	struct v2f{
		float4 pos:POSITION;
		float3 normal:TEXCOORD;
		float3 worldPos:TEXCOORD1;
	};
	
	v2f vert(appdata_base i){
		v2f o;
		o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
		o.normal = mul(float4(i.normal,0),_World2Object);
		o.worldPos = mul(_Object2World,o.pos).xyz;
		return o;
	}
	
	float4 frag(v2f i):COLOR{
		float4 c = (0,0,0,0);
		
		float3 normal = normalize(i.normal).xyz;
		
		float attenuation = 1.0;
		float3 lightDir = _WorldSpaceLightPos0.xyz;
		// #ifndef USING_DIRECTIONAL_LIGHT //point,spot
			// lightDir = _WorldSpaceLightPos0.xyz - i.worldPos;
			// attenuation = 1.0 / length(lightDir);//saturate(1.0 - length(lightDir));
		// #endif
		// c = float4(_WorldSpaceLightPos0.w);
		lightDir = normalize(lightDir);
		
		float3 reflectDir = normalize(reflect(-lightDir,normal));
		
		float3 viewDir = normalize(_WorldSpaceCameraPos.xyz);
		float3 h = normalize(lightDir + viewDir);
		float nl = max(0,dot(normal,lightDir));
		float vr = max(0,dot(viewDir,reflectDir));
		float nh = max(0,dot(normal,h));
		float spec = pow(nh,_SpecularPower);
		
		float4 diffuseLight = _DiffuseColor  * _DiffuseIntensity * dot(normal,lightDir);
		float4 specularLight = _SpecularColor  * _SpecularIntensity * spec;
		
		c = _LightIntensity * diffuseLight + specularLight;
		return c;
	}
	
	ENDCG
	
	SubShader {
		Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
		LOD 200
		Pass{
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
